
#include <cstdio>
#include <BStyle>
#include <BListItem>
#include <BHBoxLayout>

#include "ObjectPanel.h"
#include "GroupPanel.h"
#include "NodePanel.h"
#include "ShapePanel.h"
#include "PropertyBoard.h"

PropertyBoard::PropertyBoard(BWidget* parent) : BScrollWidget(parent)
{
	this->setAreaPolicy(Policy_Dynamic, Policy_Preferred);

	_typedPanelMap["BObject"] = new ObjectPanel();
	_typedPanelMap["BGroup"] = new GroupPanel();
	_typedPanelMap["BNode"] = new NodePanel();
	_typedPanelMap["BShape"] = new ShapePanel();

	_listWidget = new BListWidget();
	{
		BLabel* titleLabel = new BLabel("Selected Objects");
		titleLabel->setStyleSheet("background:empty");
		BHBoxLayout* titleLayout = new BHBoxLayout();
		titleLayout->addWidget(titleLabel);
		_listWidget->setLayout(titleLayout, Part_Top);
	}

	connect("target-changed", &PropertyBoard::slotTargetChanged);
	connect(this, Signal_AssetChanged, &PropertyBoard::slotAssetChanged);
}
PropertyBoard::~PropertyBoard()
{

}

void PropertyBoard::setScene(BScene* scene)
{
	_scene = scene;
}
BScene* PropertyBoard::scene() const
{
	return _scene;
}

void PropertyBoard::freshListItemIcons()
{
	const BStyle* style = this->realStyle();
	BIcon* unknownIcon = style->icon("unknown");
	for (int i = 0; i < _listWidget->itemCount(); i++)
	{
		BListItem* item = _listWidget->item(i);
		BObject* object = item->asset();
		BIcon* typedIcon = style->icon(object->className());
		if (typedIcon)
			item->setIcon(typedIcon);
		else
			item->setIcon(unknownIcon);
	}
}

void PropertyBoard::slotAssetChanged(BObject* object, const BValue& value)
{
	BObject* obj = value;
	for (int i = 0; i < this->widgetCount(); i++)
	{
		BWidget* panel = this->widget(i);
		panel->setAsset(BValue());
	}
	this->clearWidgets();
	if (obj == 0)
	{
		_listWidget->clearItems();
		this->addWidget(_listWidget);
		return;
	}
	for (int i = 0; i < obj->typeCount(); i++)
	{
		const char* type = obj->typeName(i);
		if (BGroupBox* panel = _typedPanelMap[type])
		{
			panel->setAsset(value);
			this->addWidget(panel);
		}
	}
}
void PropertyBoard::slotTargetChanged(BObject* object, const BValue& value)
{
	Target* target = value;
	if (target)
	{
		if (target->shapeCount() == 1)
		{
			BShape* shape = target->shape(0);
			this->setAsset(shape);
			return;
		}
		if (target->nodeCount() == 1)
		{
			BNode* node = target->node(0);
			this->setAsset(node);
			return;
		}
		if (target->groupCount() == 1)
		{
			BGroup* group = target->group(0);
			this->setAsset(group);
			return;
		}
		this->setAsset(BValue());
		this->clearWidgets();
		_listWidget->clearItems();
		if (target->shapeCount() > 1)
		{
			this->addWidget(_listWidget);
			_listWidget->clearItems();
			for (int i = 0; i < target->shapeCount(); i++)
			{
				BShape* shape = target->shape(i);
				BListItem* item = _listWidget->addItem(shape->name());
				item->setAsset(shape);
			}
			this->freshListItemIcons();
			return;
		}
		if (target->nodeCount() > 1)
		{
			this->addWidget(_listWidget);
			_listWidget->clearItems();
			for (int i = 0; i < target->nodeCount(); i++)
			{
				BNode* node = target->node(i);
				BListItem* item = _listWidget->addItem(node->name());
				item->setAsset(node);
			}
			this->freshListItemIcons();
			return;
		}
		if (target->groupCount() > 1)
		{
			this->addWidget(_listWidget);
			_listWidget->clearItems();
			for (int i = 0; i < target->groupCount(); i++)
			{
				BGroup* group = target->group(i);
				BListItem* item = _listWidget->addItem(group->name());
				item->setAsset(group);
			}
			this->freshListItemIcons();
			return;
		}
	}
}
